var app = getApp();
var url = app.url;
Page({
  data: {
    levels:"",
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    if (app.globalData.flag) { wx.navigateBack() }
  },
  onShow: function () {
    // 页面显示
    var deptName = wx.getStorageSync('deptName')
    this.setData({levels:wx.getStorageSync('levels') || ""})
    this.getUserInfo()
    // this.checkLogin()
  },
  onReady: function () {
    // 页面渲染完成
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  },
  onPullDownRefresh: function () {
    
    // this.checkLogin()
  },
  onShareAppMessage: function () {

  },
  checkLogin: function (callBack) {
    var name = wx.getStorageSync('userName')
    if (name == '' || name == null) {
      wx.showModal({
        title: '提示',
        content: "用户未登录",
        confirmText: '去登录',
        showCancel: true,
        success: function (res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '../login/login',
            })
          } else if (res.cancel) {
            wx.navigateTo({
              url: '../control/controlBooth',
            })
          }
        }

      })
    } else {
      callBack()
    }
  },
  getUserInfo: function () {
    var that = this
    wx.getUserInfo({
      lang: 'zh_CN',
      success: function (res) {
        app.userInfo = res.userInfo
        that.doLogin(res.userInfo, res.encryptedData, res.iv)
      },
      fail: function (res) {
        wx.showModal({
          title: '需要您授权才能正常显示',
          content: '请点击“去授权”并点击授权按钮，即可正常使用',
          confirmText: '去授权',
          showCancel: true,
          success: function (res) {
            if (res.confirm) {
              // wx.openSetting({
              //   success: function (re) {
              //     if (!re.authSetting["scope.userInfo"] || !re.authSetting["scope.userLocation"]) {
              //       var isset = that.data.isSetUserInfo
              //       if(!isset){
              //         that.getUserInfo()
              //       }
              //     }else{
              //       that.setData({ isSetUserInfo : true})
              //     }
              //   }
              // })
              console.log("用户点击确定")
              wx.navigateTo({
                url: '../authorize/authorize',
              })
            }
          }
        })
      }
    })
  },
  doLogin: function (userInfo, encryptedData, iv) {
    var that = this
    wx.login({
      success: function (res) {
        var params = { code: res.code, userInfo: userInfo, encryptedData: encryptedData, iv: iv }
        var encodingStr = app.ecbEncrypto(params)
        wx.request({
          url: app.url + '/web/member/recordTimes.htm',
          data: encodingStr,
          method: 'POST',
          success: function (res) {
            var decodingStr = app.ecbDecrypto(res.data)
            var rest = JSON.parse(decodingStr)
            console.log(rest)
            if (rest.isSuccess == 0) {
              app.memberId = rest.menberId
            }
          }
        })
      }
    })
  },
  goToPage(e){
    const url = e.currentTarget.dataset.url
    console.log(url)
    wx.navigateTo({url})
  },
  bindToQuery: function () {
    this.checkLogin(function(){
      wx.navigateTo({
        url: '../control/query',
      })
    })
  },
  bindToReadout: function () {
    wx.navigateTo({
      url: '../control/readout',
    })
  },
  bindToKeep: function () {
    this.checkLogin(function(){
      wx.navigateTo({
        url: '../reportList/processed',
      })
    })
  },
  bindToMonitor: function () {
    wx.navigateTo({
      url: '../control/monitor',
    })
  },
  bindEXception: function () {
    wx.navigateTo({
      url: '../innerReport/innerReport',
    })
  },
  bindEXceptionList: function () {
    this.checkLogin(function(){
      wx.navigateTo({
        url: '../reportList/reportList',
      })
    })
    
  },
  bindToZdSet: function () {
    wx.navigateTo({
      url: '../control/zdSetup',
    })
  },
  bindDaka(){
    wx.showModal({
      title: '提示',
      content: '今天是否需要打卡上班？',
      success:  (sm)=> {
        if (sm.confirm) {
          wx.request({
            url: 'https://www.huadulds.com/api/baseAccount/setSignToday?userId='+wx.getStorageSync('userId'),
            data: {},
            method: "post",
            success: function (res) {
              var res = res.data;
              console.log(res);
              if(res.code == 1){
                wx.showToast({
                  title: '打卡成功',
                  duration: 1000,
                })
              }else{
                wx.showToast({
                  title: res.msg,
                  image: '../../images/error.png',
                  duration: 1000,
                  mask: true
                })
              }
              
            }
          })
          } else if (sm.cancel) {
            console.log('用户点击取消')
          }
        }
      })
  }
});